'use strict';

var _interopRequireDefault = require('@babel/runtime/helpers/interopRequireDefault');

Object.defineProperty(exports, '__esModule', {
  value: true,
});
exports.$override = $override;
exports.guid = guid;
exports.logError = logError;
exports.clone = clone;
exports.merge = merge;
exports.mergeAll = mergeAll;
exports.extend = extend;
exports.defaults = defaults;
exports.indexOf = indexOf;
exports.inherits = inherits;
exports.mixin = mixin;
exports.isArrayLike = isArrayLike;
exports.each = each;
exports.map = map;
exports.reduce = reduce;
exports.filter = filter;
exports.find = find;
exports.keys = keys;
exports.curry = curry;
exports.isArray = isArray;
exports.isFunction = isFunction;
exports.isString = isString;
exports.isStringSafe = isStringSafe;
exports.isNumber = isNumber;
exports.isObject = isObject;
exports.isBuiltInObject = isBuiltInObject;
exports.isTypedArray = isTypedArray;
exports.isDom = isDom;
exports.isGradientObject = isGradientObject;
exports.isImagePatternObject = isImagePatternObject;
exports.isRegExp = isRegExp;
exports.eqNaN = eqNaN;
exports.retrieve = retrieve;
exports.retrieve2 = retrieve2;
exports.retrieve3 = retrieve3;
exports.slice = slice;
exports.normalizeCssArray = normalizeCssArray;
exports.assert = assert;
exports.trim = trim;
exports.setAsPrimitive = setAsPrimitive;
exports.isPrimitive = isPrimitive;
exports.createHashMap = createHashMap;
exports.concatArray = concatArray;
exports.createObject = createObject;
exports.hasOwn = hasOwn;
exports.noop = noop;
exports.HashMap = exports.bind = exports.createCanvas = void 0;

var _typeof2 = _interopRequireDefault(require('@babel/runtime/helpers/typeof'));

var BUILTIN_OBJECT = {
  '[object Function]': true,
  '[object RegExp]': true,
  '[object Date]': true,
  '[object Error]': true,
  '[object CanvasGradient]': true,
  '[object CanvasPattern]': true,
  '[object Image]': true,
  '[object Canvas]': true,
};
var TYPED_ARRAY = {
  '[object Int8Array]': true,
  '[object Uint8Array]': true,
  '[object Uint8ClampedArray]': true,
  '[object Int16Array]': true,
  '[object Uint16Array]': true,
  '[object Int32Array]': true,
  '[object Uint32Array]': true,
  '[object Float32Array]': true,
  '[object Float64Array]': true,
};
var objToString = Object.prototype.toString;
var arrayProto = Array.prototype;
var nativeForEach = arrayProto.forEach;
var nativeFilter = arrayProto.filter;
var nativeSlice = arrayProto.slice;
var nativeMap = arrayProto.map;

var ctorFunction = function () {}.constructor;

var protoFunction = ctorFunction ? ctorFunction.prototype : null;
var methods = {};

function $override(name, fn) {
  methods[name] = fn;
}

var idStart = 0x0907;

function guid() {
  return idStart++;
}

function logError() {
  var args = [];

  for (var _i = 0; _i < arguments.length; _i++) {
    args[_i] = arguments[_i];
  }

  if (typeof console !== 'undefined') {
    console.error.apply(console, args);
  }
}

function clone(source) {
  if (source == null || (0, _typeof2['default'])(source) !== 'object') {
    return source;
  }

  var result = source;
  var typeStr = objToString.call(source);

  if (typeStr === '[object Array]') {
    if (!isPrimitive(source)) {
      result = [];

      for (var i = 0, len = source.length; i < len; i++) {
        result[i] = clone(source[i]);
      }
    }
  } else if (TYPED_ARRAY[typeStr]) {
    if (!isPrimitive(source)) {
      var Ctor = source.constructor;

      if (Ctor.from) {
        result = Ctor.from(source);
      } else {
        result = new Ctor(source.length);

        for (var i = 0, len = source.length; i < len; i++) {
          result[i] = clone(source[i]);
        }
      }
    }
  } else if (
    !BUILTIN_OBJECT[typeStr] &&
    !isPrimitive(source) &&
    !isDom(source)
  ) {
    result = {};

    for (var key in source) {
      if (source.hasOwnProperty(key)) {
        result[key] = clone(source[key]);
      }
    }
  }

  return result;
}

function merge(target, source, overwrite) {
  if (!isObject(source) || !isObject(target)) {
    return overwrite ? clone(source) : target;
  }

  for (var key in source) {
    if (source.hasOwnProperty(key)) {
      var targetProp = target[key];
      var sourceProp = source[key];

      if (
        isObject(sourceProp) &&
        isObject(targetProp) &&
        !isArray(sourceProp) &&
        !isArray(targetProp) &&
        !isDom(sourceProp) &&
        !isDom(targetProp) &&
        !isBuiltInObject(sourceProp) &&
        !isBuiltInObject(targetProp) &&
        !isPrimitive(sourceProp) &&
        !isPrimitive(targetProp)
      ) {
        merge(targetProp, sourceProp, overwrite);
      } else if (overwrite || !(key in target)) {
        target[key] = clone(source[key]);
      }
    }
  }

  return target;
}

function mergeAll(targetAndSources, overwrite) {
  var result = targetAndSources[0];

  for (var i = 1, len = targetAndSources.length; i < len; i++) {
    result = merge(result, targetAndSources[i], overwrite);
  }

  return result;
}

function extend(target, source) {
  if (Object.assign) {
    Object.assign(target, source);
  } else {
    for (var key in source) {
      if (source.hasOwnProperty(key)) {
        target[key] = source[key];
      }
    }
  }

  return target;
}

function defaults(target, source, overlay) {
  var keysArr = keys(source);

  for (var i = 0; i < keysArr.length; i++) {
    var key = keysArr[i];

    if (overlay ? source[key] != null : target[key] == null) {
      target[key] = source[key];
    }
  }

  return target;
}

var createCanvas = function createCanvas() {
  return methods.createCanvas();
};

exports.createCanvas = createCanvas;

methods.createCanvas = function () {
  return document.createElement('canvas');
};

function indexOf(array, value) {
  if (array) {
    if (array.indexOf) {
      return array.indexOf(value);
    }

    for (var i = 0, len = array.length; i < len; i++) {
      if (array[i] === value) {
        return i;
      }
    }
  }

  return -1;
}

function inherits(clazz, baseClazz) {
  var clazzPrototype = clazz.prototype;

  function F() {}

  F.prototype = baseClazz.prototype;
  clazz.prototype = new F();

  for (var prop in clazzPrototype) {
    if (clazzPrototype.hasOwnProperty(prop)) {
      clazz.prototype[prop] = clazzPrototype[prop];
    }
  }

  clazz.prototype.constructor = clazz;
  clazz.superClass = baseClazz;
}

function mixin(target, source, override) {
  target = 'prototype' in target ? target.prototype : target;
  source = 'prototype' in source ? source.prototype : source;

  if (Object.getOwnPropertyNames) {
    var keyList = Object.getOwnPropertyNames(source);

    for (var i = 0; i < keyList.length; i++) {
      var key = keyList[i];

      if (key !== 'constructor') {
        if (override ? source[key] != null : target[key] == null) {
          target[key] = source[key];
        }
      }
    }
  } else {
    defaults(target, source, override);
  }
}

function isArrayLike(data) {
  if (!data) {
    return false;
  }

  if (typeof data === 'string') {
    return false;
  }

  return typeof data.length === 'number';
}

function each(arr, cb, context) {
  if (!(arr && cb)) {
    return;
  }

  if (arr.forEach && arr.forEach === nativeForEach) {
    arr.forEach(cb, context);
  } else if (arr.length === +arr.length) {
    for (var i = 0, len = arr.length; i < len; i++) {
      cb.call(context, arr[i], i, arr);
    }
  } else {
    for (var key in arr) {
      if (arr.hasOwnProperty(key)) {
        cb.call(context, arr[key], key, arr);
      }
    }
  }
}

function map(arr, cb, context) {
  if (!arr) {
    return [];
  }

  if (!cb) {
    return slice(arr);
  }

  if (arr.map && arr.map === nativeMap) {
    return arr.map(cb, context);
  } else {
    var result = [];

    for (var i = 0, len = arr.length; i < len; i++) {
      result.push(cb.call(context, arr[i], i, arr));
    }

    return result;
  }
}

function reduce(arr, cb, memo, context) {
  if (!(arr && cb)) {
    return;
  }

  for (var i = 0, len = arr.length; i < len; i++) {
    memo = cb.call(context, memo, arr[i], i, arr);
  }

  return memo;
}

function filter(arr, cb, context) {
  if (!arr) {
    return [];
  }

  if (!cb) {
    return slice(arr);
  }

  if (arr.filter && arr.filter === nativeFilter) {
    return arr.filter(cb, context);
  } else {
    var result = [];

    for (var i = 0, len = arr.length; i < len; i++) {
      if (cb.call(context, arr[i], i, arr)) {
        result.push(arr[i]);
      }
    }

    return result;
  }
}

function find(arr, cb, context) {
  if (!(arr && cb)) {
    return;
  }

  for (var i = 0, len = arr.length; i < len; i++) {
    if (cb.call(context, arr[i], i, arr)) {
      return arr[i];
    }
  }
}

function keys(obj) {
  if (!obj) {
    return [];
  }

  if (Object.keys) {
    return Object.keys(obj);
  }

  var keyList = [];

  for (var key in obj) {
    if (obj.hasOwnProperty(key)) {
      keyList.push(key);
    }
  }

  return keyList;
}

function bindPolyfill(func, context) {
  var args = [];

  for (var _i = 2; _i < arguments.length; _i++) {
    args[_i - 2] = arguments[_i];
  }

  return function () {
    return func.apply(context, args.concat(nativeSlice.call(arguments)));
  };
}

var bind =
  protoFunction && isFunction(protoFunction.bind)
    ? protoFunction.call.bind(protoFunction.bind)
    : bindPolyfill;
exports.bind = bind;

function curry(func) {
  var args = [];

  for (var _i = 1; _i < arguments.length; _i++) {
    args[_i - 1] = arguments[_i];
  }

  return function () {
    return func.apply(this, args.concat(nativeSlice.call(arguments)));
  };
}

function isArray(value) {
  if (Array.isArray) {
    return Array.isArray(value);
  }

  return objToString.call(value) === '[object Array]';
}

function isFunction(value) {
  return typeof value === 'function';
}

function isString(value) {
  return typeof value === 'string';
}

function isStringSafe(value) {
  return objToString.call(value) === '[object String]';
}

function isNumber(value) {
  return typeof value === 'number';
}

function isObject(value) {
  var type = (0, _typeof2['default'])(value);
  return type === 'function' || (!!value && type === 'object');
}

function isBuiltInObject(value) {
  return !!BUILTIN_OBJECT[objToString.call(value)];
}

function isTypedArray(value) {
  return !!TYPED_ARRAY[objToString.call(value)];
}

function isDom(value) {
  return (
    (0, _typeof2['default'])(value) === 'object' &&
    typeof value.nodeType === 'number' &&
    (0, _typeof2['default'])(value.ownerDocument) === 'object'
  );
}

function isGradientObject(value) {
  return value.colorStops != null;
}

function isImagePatternObject(value) {
  return value.image != null;
}

function isRegExp(value) {
  return objToString.call(value) === '[object RegExp]';
}

function eqNaN(value) {
  return value !== value;
}

function retrieve() {
  var args = [];

  for (var _i = 0; _i < arguments.length; _i++) {
    args[_i] = arguments[_i];
  }

  for (var i = 0, len = args.length; i < len; i++) {
    if (args[i] != null) {
      return args[i];
    }
  }
}

function retrieve2(value0, value1) {
  return value0 != null ? value0 : value1;
}

function retrieve3(value0, value1, value2) {
  return value0 != null ? value0 : value1 != null ? value1 : value2;
}

function slice(arr) {
  var args = [];

  for (var _i = 1; _i < arguments.length; _i++) {
    args[_i - 1] = arguments[_i];
  }

  return nativeSlice.apply(arr, args);
}

function normalizeCssArray(val) {
  if (typeof val === 'number') {
    return [val, val, val, val];
  }

  var len = val.length;

  if (len === 2) {
    return [val[0], val[1], val[0], val[1]];
  } else if (len === 3) {
    return [val[0], val[1], val[2], val[1]];
  }

  return val;
}

function assert(condition, message) {
  if (!condition) {
    throw new Error(message);
  }
}

function trim(str) {
  if (str == null) {
    return null;
  } else if (typeof str.trim === 'function') {
    return str.trim();
  } else {
    return str.replace(/^[\s\uFEFF\xA0]+|[\s\uFEFF\xA0]+$/g, '');
  }
}

var primitiveKey = '__ec_primitive__';

function setAsPrimitive(obj) {
  obj[primitiveKey] = true;
}

function isPrimitive(obj) {
  return obj[primitiveKey];
}

var HashMap = (function () {
  function HashMap(obj) {
    this.data = {};
    var isArr = isArray(obj);
    this.data = {};
    var thisMap = this;
    obj instanceof HashMap ? obj.each(visit) : obj && each(obj, visit);

    function visit(value, key) {
      isArr ? thisMap.set(value, key) : thisMap.set(key, value);
    }
  }

  HashMap.prototype.get = function (key) {
    return this.data.hasOwnProperty(key) ? this.data[key] : null;
  };

  HashMap.prototype.set = function (key, value) {
    return (this.data[key] = value);
  };

  HashMap.prototype.each = function (cb, context) {
    for (var key in this.data) {
      if (this.data.hasOwnProperty(key)) {
        cb.call(context, this.data[key], key);
      }
    }
  };

  HashMap.prototype.keys = function () {
    return keys(this.data);
  };

  HashMap.prototype.removeKey = function (key) {
    delete this.data[key];
  };

  return HashMap;
})();

exports.HashMap = HashMap;

function createHashMap(obj) {
  return new HashMap(obj);
}

function concatArray(a, b) {
  var newArray = new a.constructor(a.length + b.length);

  for (var i = 0; i < a.length; i++) {
    newArray[i] = a[i];
  }

  var offset = a.length;

  for (var i = 0; i < b.length; i++) {
    newArray[i + offset] = b[i];
  }

  return newArray;
}

function createObject(proto, properties) {
  var obj;

  if (Object.create) {
    obj = Object.create(proto);
  } else {
    var StyleCtor = function StyleCtor() {};

    StyleCtor.prototype = proto;
    obj = new StyleCtor();
  }

  if (properties) {
    extend(obj, properties);
  }

  return obj;
}

function hasOwn(own, prop) {
  return own.hasOwnProperty(prop);
}

function noop() {}
